﻿using PiControls.Controls.DriveUI.TCPClient;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace PiControls.Controls.DriveUI
{
    public partial class addDriveSelect: Form
    {

        public string AddObjectName { get; private set; } // 存储用户输入的文本
        public addDriveSelect()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent; // 让对话框居中于父窗口
            string[] comboBoxItems = { "TCP客户端", "Modbus客户端", "Modbus服务器", "TCP服务器", "串口通信" };
            comboBox1.Items.AddRange(comboBoxItems);

            this.Padding = new Padding(5); // 设置边框厚度
            this.BackColor = Color.White; // 设置内部背景色
            this.Paint += IaddDriveSelect_Paint;

        }

        private void IaddDriveSelect_Paint(object sender, PaintEventArgs e)
        {
            int borderWidth = 15; // 边框宽度
            using (Pen pen = new Pen(Color.Red, borderWidth)) // 红色边框
            {
                e.Graphics.DrawRectangle(pen,
                    borderWidth / 2, borderWidth / 2,
                    this.ClientSize.Width - borderWidth,
                    this.ClientSize.Height - borderWidth);
            }
     
        }

        TcpClientConfig TcpConfig = null;
        public string ObjectName { get; private set; } // 存储用户输入的文本
        public string ip { get; private set; } // 存储用户输入的文本
        public int port { get; private set; } // 存储用户输入的文本
        public bool isRunOpen { get; private set; } // 存储用户输入的文本
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedItem = comboBox1.SelectedItem.ToString();
            Console.WriteLine(selectedItem);
            if (selectedItem == "TCP客户端")
            {
                if (TcpConfig != null) { 


                    tableLayoutPanel1.Controls.Remove(TcpConfig);
                    TcpConfig.Dispose();
                    TcpConfig = null;
                }

                TcpConfig = new TcpClientConfig();
                tableLayoutPanel1.Controls.Add(TcpConfig, 0, 1); // 第一列 第二行
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "TCP客户端")
            {
               bool isOk  =TcpConfig.saveConfig();
                if (!isOk)
                {
                    MessageBox.Show("配置信息有误，请检查！");
                    return;
                }
                ip = TcpConfig.getIP();
                port = TcpConfig.getPort();
                isRunOpen = TcpConfig.getRunIsConnected();
                ObjectName = TcpConfig.getObjectName();
                Console.WriteLine(ip + " " + port + " " + isRunOpen + " " + ObjectName);
                AddObjectName = "TCP客户端";
            }

            //this.dialogResult = DialogResult.OK;
            this.DialogResult = DialogResult.OK;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
